﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTLib.Common;
using DCTAccess.Business.News;
using DCSLibrary;
using BDS;
using Utils.DCSControl;
using Utils;
using Utils.DatabaseUtils;
using DCTAccess.Business.Member;

public partial class Members_chinh_sua_tin_dang : System.Web.UI.Page
{
    CategoryPageUI objFunction = new CategoryPageUI();
    BDS.ProductUI objProduct = new BDS.ProductUI();
    NeedsUI objNeeds = new NeedsUI();
    LocationUI objLocation = new LocationUI();
    DataSet dsLocation;
    DataSet dsFunctionpage;
    dcPage page;
    protected int newID;
    protected void Page_Load(object sender, EventArgs e)
    {
        dsLocation = objLocation.GetAll();
        dsFunctionpage = objFunction.GetAll();
        //page = (dcPage)this.Page;
        //if (page.CurrentUser == null)
        //{
        //    MyMessage.ShowMessage("Ban can phai dang nhap de su dung chuc nang nay");
        //    Response.Write("<script>window.location.href = '/'</script>");
        //}
        Upload2.UploadFolderPath = "~/Upload/Content";
        Upload2.OnUploadCompleted += Upload2_OnUploadCompleted;
        if (!IsPostBack)
        {
            BindChuyenMuc();
            BindNoiRao();
            BindData();
        }
    }

    private void Upload2_OnUploadCompleted(string selectedFile)
    {
        txtProductImage.Text = selectedFile.Substring(2).ToString();
        if (txtProductImage.Text != "")
        { }
    }

    protected void BindData()
    {
        newID = AppRequestInfo.ID;
        lblInfo.Text = newID.ToString();

        if (newID != -1)
        {
            News sbmNews = NewsControler.GetNewsByID(newID);
            if (sbmNews != null)
            {
                txtProductID.Text = sbmNews.ProductID.ToString();
                txtProductName.Text = sbmNews.Title;
                txtProductImage.Text = sbmNews.Images;                
                txtProductContent.Content = sbmNews.Noidung;                
            }
        }
    }
    protected void cboChuyenMuc_Changed(object sender, EventArgs e)
    {
        object obj = ((DropDownList)sender).SelectedValue;
        if (obj != null)
        {
            int cateId = Convert.ToInt32(obj);
            BindChungLoai(cateId);
        }
    }

    private void BindChungLoai(int cateId)
    {
        cboNeeds.DataSource = objNeeds.GetByCategoryPageId(cateId);
        cboNeeds.DataBind();

        cboChungLoai.DataSource = TableLib.CreateTable(dsFunctionpage.Tables[0], "ParentID = " + cateId);
        cboChungLoai.DataBind();
    }

    protected void cboNoiRao_Changed(object sender, EventArgs e)
    {
        object obj = ((DropDownList)sender).SelectedValue;
        if (obj != null)
        {
            int locationId = Convert.ToInt32(obj);
            BindQuanHuyen(locationId);
        }
    }

    private void BindQuanHuyen(int locationId)
    {
        cboQuanHuyen.DataSource = TableLib.CreateTable(dsLocation.Tables[0], "ParentID = " + locationId);
        cboQuanHuyen.DataBind();
    }

    private void BindChuyenMuc()
    {
        try
        {
            cboChuyenMuc.DataSource = TableLib.CreateTable(dsFunctionpage.Tables[0], "ParentID = " + ConfigurationManager.AppSettings["NewsID"]);
            cboChuyenMuc.DataBind();
        }
        catch (Exception e)
        {
        }
    }

    private void BindNoiRao()
    {
        try
        {
            DataTable dt = TableLib.CreateTable(dsLocation.Tables[0], "ParentID is null");
            cboNoiRao.DataSource = dt;
            cboNoiRao.DataBind();
        }
        catch (Exception e)
        {
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Member memInfo = MemberController.GetMemberByUsername(Session["User"].ToString());
        int uID = memInfo.ID;
        int locationId = 0;
        if (cboNoiRao.SelectedValue != "0")
        {
            if (cboQuanHuyen.SelectedValue != "0")
            {
                locationId = Convert.ToInt32(cboQuanHuyen.SelectedValue);
            }
            else
            {
                locationId = Convert.ToInt32(cboNoiRao.SelectedValue);
            }
        }
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtProductName.Text.Trim());
        string urlPath = "/" + Utils.Format.ConvertToNoSign(cboChuyenMuc.SelectedItem.Text) + "/"
                        + Utils.Format.ConvertToNoSign(cboChungLoai.SelectedItem.Text) + "/"
                        + Utils.Format.ConvertToNoSign(cboNeeds.SelectedItem.Text) + "/"
                        + Utils.Format.ConvertToNoSign(cboNoiRao.SelectedItem.Text) + "/"
                        + Utils.Format.ConvertToNoSign(cboQuanHuyen.SelectedItem.Text) + "/"
                        + Utils.Format.ConvertToNoSign(txtProductName.Text) + ".aspx";
        try
        {
            //int result = objProduct.Add(Convert.ToInt32(cboChungLoai.SelectedValue), locationId,
            //    Convert.ToInt32(cboNeeds.SelectedValue), urlPath, "", "", txtProductName.Text.Trim(), alias, txtProductContent.Content, txtProductImage.Text.Trim(), "", Convert.ToDecimal(txtProductPrice.Text.Trim()), cboCurrency.SelectedValue, 0, 0, 0, 0, 0, 1, 1, DateTime.Now, uID);
            int result = objProduct.Update(Convert.ToInt32(txtProductID.Text),Convert.ToInt32(cboChungLoai.SelectedValue), locationId,
                Convert.ToInt32(cboNeeds.SelectedValue), urlPath, "", "", txtProductName.Text.Trim(), alias, txtProductContent.Content, txtProductImage.Text.Trim(), "", Convert.ToDecimal(txtProductPrice.Text.Trim()), cboCurrency.SelectedValue, 0, 0, 0, 0, 0);
            
            if (result > 0)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                MyMessage.ShowError("Cập nhật thành công");
            }
            else
            {
                MyMessage.ShowError("Có lỗi trong quá trình đăng tin, xin hãy thử lại");
            }
        }
        catch (Exception ex)
        {
            //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
            //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
            MyMessage.ShowError("Du lieu ban nhap khong chinh xac! Ban vui long kiem tra lai thong tin");
        }
    }
   
    protected void cboPrice_Changed(object sender, EventArgs e)
    {
        if (cboPrice.SelectedValue == "1")
        {
            txtProductPrice.Visible = false;
            cboCurrency.Visible = false;
            txtProductPrice.Text = "-1";
        }
        else
        {
            txtProductPrice.Visible = true;
            cboCurrency.Visible = true;
            txtProductPrice.Text = "";
        }
    }
}
